package unwrap;

import processing.core.PGraphics;
import toxi.geom.Vec2D;
import toxi.geom.Vec3D;

public class USide {

	/**
	 * home triangle
	 */
	public final UTriangle triangle;
	/**
	 * neighbor triangle
	 */
	public UTriangle neighbor = null;
	/**
	 * side length
	 */
	public float length;
	UPoint p1, p2;
	UConnection c;
	/**
	 * always opposite to the neighbor side
	 */
	public boolean sideState = false;

	
	public USide(UPoint _p1, UPoint _p2, UTriangle _t) {
		triangle = _t;
		setPoints(_p1, _p2);
	}

	/**
	 * updates the referred points & distance between them
	 */
	void setPoints(UPoint a, UPoint b) {
		p1 = a;
		p2 = b;
		length = p1.distanceTo(p2);
	}
	
	/**
	 * get the angle between the two triangles
	 */
	public float getAngle() {
		Vec3D tp = p1.interpolateTo(p2, 0.5f);
		c = new UConnection(this, tp);
		return c.getAngle();
	}

	/**
	 * get the neighbor side
	 */
	public USide getNeighborSide() {
		USide neighborSide = null;

		if (neighbor != null) {
			if (sideMatch(neighbor.getAB(), triangle.id) == true)
				neighborSide = neighbor.getAB();
			else if (sideMatch(neighbor.getBC(), triangle.id) == true)
				neighborSide = neighbor.getBC();
			else if (sideMatch(neighbor.getCA(), triangle.id) == true)
				neighborSide = neighbor.getCA();
		}
		return neighborSide;
	}

	private boolean sideMatch(USide ns, int id) {
		boolean sideMatch = false;
		if (ns.neighbor != null) {
			if (ns.neighbor.id == id)
				sideMatch = true;
		}
		return sideMatch;
	}
}
